﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.IO;
using System.Web.Routing;

namespace JQMdotNet
{
    static public class UniversalTagHelper
    {
        /// <summary>
        /// Creates an MVC tag based with the given html attributes
        /// </summary>
        /// <param name="tag">The tag name</param>
        /// <param name="htmlAttributes">Dictionary of html attibutes</param>
        /// <param name="selfClosing">true if the tag is self closing</param>
        /// <returns>Html to render the tag</returns>
        static public IHtmlString RenderBeginTag(string tag, IDictionary<string, object> htmlAttributes, bool selfClosing)
        {
            TagBuilder builder = new TagBuilder(tag);
            builder.MergeAttributes(htmlAttributes);

            TagRenderMode mode = selfClosing ? TagRenderMode.SelfClosing : TagRenderMode.StartTag;
            return new MvcHtmlString(builder.ToString(mode));
        }

        /// <summary>
        /// Creates an MVC tag based with the given html attributes
        /// </summary>
        /// <param name="tag">The tag name</param>
        /// <param name="htmlAttributes">Dictionary of html attibutes</param>
        /// <param name="selfClosing">true if the tag is self closing</param>
        /// <returns>Html to render the tag</returns>
        static public IHtmlString RenderBeginTag(string tag, object htmlAttributes, bool selfClosing)
        {
            return RenderBeginTag(tag, new RouteValueDictionary(htmlAttributes), selfClosing);
        }

        /// <summary>
        /// Creates an MVC tag based with the given html attributes
        /// </summary>
        /// <param name="tag">The tag name</param>
        /// <param name="htmlAttributes">Dictionary of html attibutes</param>
        /// <param name="selfClosing">true if the tag is self closing</param>
        /// <returns>Html to render the tag</returns>
        static public IHtmlString RenderBeginTag(string tag, object htmlAttributes)
        {
            return RenderBeginTag(tag, new RouteValueDictionary(htmlAttributes), false);
        }

        /// <summary>
        /// Creates an Html tag based with the given html attributes
        /// </summary>
        /// <param name="tag">The tag name</param>
        /// <returns>Html to render the tag</returns>
        static public IHtmlString RenderEndTag(string tag)
        {
            return new MvcHtmlString("</" + tag + ">");
        }


        static public UniversalTag BeginTag(this HtmlHelper helper, string tag, IDictionary<string, object> htmlAttributes)
        {
            helper.ViewContext.Writer.Write(RenderBeginTag(tag, htmlAttributes, false));
            return new UniversalTag(helper, tag);
        }

        static public void EndTag(this HtmlHelper helper, string tag)
        {
            helper.ViewContext.Writer.Write(RenderEndTag(tag));
        }
    }

    public class UniversalTag : IDisposable
    {
        private string _tagName;
        private HtmlHelper _helper;

        public UniversalTag(HtmlHelper helper, string tag)
        {
            _tagName = tag;
            _helper = helper;
        }

        public void Dispose()
        {
            _helper.ViewContext.Writer.Write(UniversalTagHelper.RenderEndTag(_tagName));
        }


    }

    public class NestedUniversalTag : IDisposable
    {
        private Stack<string> _tagNames;
        private HtmlHelper _helper;

        public NestedUniversalTag(HtmlHelper helper, IEnumerable<string> tags)
        {
            _tagNames = new Stack<string>(tags);
            _helper = helper;
        }


        public void Dispose()
        {
            while (_tagNames.Count > 0)
            {
                string tagName = _tagNames.Pop();
                _helper.ViewContext.Writer.Write(UniversalTagHelper.RenderEndTag(tagName));
            }
        }
    }
}